package online.sh.cn.fy.users.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.message.SendMessage;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;

/**
 * S发送短信
 */
@WebServlet("/message")
public class MessageSendServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String mobile=request.getParameter("mobile");
		String lockcode=request.getParameter("lockcode");
		String exlockcode=request.getSession().getAttribute("lockcode").toString().toLowerCase();
		String coder=Integer.toString((int)((Math.random()*9+1)*100000));
		TokenUtils tu=new TokenUtils();
		String checkmobile=tu.getMobileBand(mobile);
		if(null!=lockcode && ""!=lockcode && exlockcode.equals(lockcode.toLowerCase())){
			if (null!=checkmobile && ""!=checkmobile) {			//认证用户手机号
				String result=SendMessage.sendMessage(mobile, coder);
				if (result.contains("result=0")) {
					try {
						mobile=DES3Util.encrypt(mobile);
						coder=DES3Util.encrypt(coder);
					} catch (Exception e) {
						log(e.getMessage());
					}
					CookieUtil.saveCookie(response, "messageMobile", mobile);	//发送验证码手机
					CookieUtil.saveCookie(response, "messageCode", coder);		//发送验证码
					JSONObject json=new JSONObject();
					json.put("result","0");
//					json.put("changeMobile", mobile);
					ResponseUtils.renderJson(response, json.toString()); 
				}else{
					JSONObject json=new JSONObject();
					json.put("result","1");
					ResponseUtils.renderJson(response, json.toString()); 
				}
			}else{
				String result=SendMessage.sendMessage(mobile, coder);
				if (result.contains("result=0")) {
					try {
						mobile=DES3Util.encrypt(mobile);
						coder=DES3Util.encrypt(coder);
					} catch (Exception e) {
						log(e.getMessage());
					}
					CookieUtil.saveCookie(response, "messageMobile", mobile);	//发送验证码手机
					CookieUtil.saveCookie(response, "messageCode", coder);		//发送验证码
					JSONObject json=new JSONObject();
					json.put("result","0");
//					json.put("changeMobile", mobile);
					ResponseUtils.renderJson(response, json.toString()); 
				}else{
					JSONObject json=new JSONObject();
					json.put("result","1");
					ResponseUtils.renderJson(response, json.toString()); 
				}
			}
		}else{
			JSONObject json=new JSONObject();
			json.put("result","3");
			ResponseUtils.renderJson(response, json.toString());
		}
	}
}
